﻿using System;
using UnityEngine;
using UnityEngine.UI;

namespace ZyGame.UI
{
    public class UIListViewItem : IDisposable
    {
        public GameObject item { get; }
        public object data { get; private set; }

        public UIListViewItem(GameObject org)
        {
            item = org;
        }

        internal void Init(object data)
        {
            this.data = data;
        }

        public void Dispose()
        {
            GameObject.DestroyImmediate(item);
        }

        public UIListViewItem Active(string name, bool active)
        {
            GameObject temp = GetChild(name);
            if (temp != null)
            {
                temp.SetActive(active);
            }

            return this;
        }

        public UIListViewItem Enable()
        {
            item?.SetActive(true);
            return this;
        }

        public UIListViewItem Disable()
        {
            item?.SetActive(false);
            return this;
        }

        public GameObject GetChild(string name)
        {
            return name == item.name ? item : this.item.transform.Find(name)?.gameObject;
        }

        public UIListViewItem OnClick(string name, Action<object> action, string soundPath = "")
        {
            GameObject gameObject = GetChild(name);
            if (gameObject == null)
            {
                return this;
            }

            Button btn = gameObject.GetComponent<Button>();
            if (btn == null)
            {
                return this;
            }

            btn.onClick.RemoveAllListeners();
            btn.onClick.AddListener(() =>
            {
                if (soundPath.IsNullOrEmpty() is false)
                {
                    Client.Music.PlayOneShortEffectSound(soundPath);
                }

                action(data);
            });
            return this;
        }

        /// <summary>
        /// 设置图片
        /// </summary>
        /// <param name="view"></param>
        /// <param name="name"></param>
        /// <param name="sprite"></param>
        /// <returns></returns>
        public UIListViewItem SetSprite(string name, string sprite)
        {
            GameObject gameObject = GetChild(name);
            if (gameObject == null)
            {
                return this;
            }

            gameObject.SetSprite(sprite);
            return this;
        }

        /// <summary>
        /// 设置图片
        /// </summary>
        /// <param name="view"></param>
        /// <param name="name"></param>
        /// <param name="texture"></param>
        /// <returns></returns>
        public UIListViewItem SetTexture2D(string name, string texture)
        {
            GameObject gameObject = GetChild(name);
            if (gameObject == null)
            {
                return this;
            }

            gameObject.SetTexture2D(texture);
            return this;
        }

        /// <summary>
        /// 设置图片
        /// </summary>
        /// <param name="view"></param>
        /// <param name="name"></param>
        /// <param name="sprite"></param>
        /// <returns></returns>
        public UIListViewItem SetSpriteAsync(string name, string sprite)
        {
            GameObject gameObject = GetChild(name);
            if (gameObject == null)
            {
                return this;
            }

            gameObject.SetSpriteAsync(sprite);
            return this;
        }

        /// <summary>
        /// 设置图片
        /// </summary>
        /// <param name="view"></param>
        /// <param name="name"></param>
        /// <param name="texture"></param>
        /// <returns></returns>
        public UIListViewItem SetTexture2DAsync(string name, string texture)
        {
            GameObject gameObject = GetChild(name);
            if (gameObject == null)
            {
                return this;
            }

            gameObject.SetTexture2DAsync(texture);
            return this;
        }

        public UIListViewItem SetText(string name, object value)
        {
            GameObject gameObject = GetChild(name);
            if (gameObject == null)
            {
                return this;
            }

            gameObject.SetText(value);
            return this;
        }
    }
}